/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsclient;

/**
 *
 * @author dvtrung
 */
import com.sun.jmx.snmp.Enumerated;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author MINHHOANG
 */
public class RunningGUI implements ActionListener {
    static JFrame frameRunning;
    JLabel labelHostname;
    JLabel labelGateway;
    JLabel labelRefreshtime;
    public static ArrayList<NetworkInterface> nets;
    private AutoUpdate auto;
    private boolean permission;

    private JComboBox comboboxHostname ;
    JComboBox comboboxGateway;
    private JLabel labelNextCheck;
    private JLabel labelStatus;
    private JLabel labelTimeRemaining;
    private JComboBox comboNetCards ;
    JButton buttonRefreshNow;

    public RunningGUI()
    {
        comboboxHostname = new JComboBox();
        comboboxGateway = new JComboBox();
        labelHostname = new JLabel("Allow hosts : ");
        labelGateway = new JLabel("Gateway : ");
        labelRefreshtime = new JLabel("Refresh Time : ");
        permission = false;
        labelNextCheck = new JLabel();
        labelStatus = new JLabel("Running...");
        labelTimeRemaining = new JLabel("...");
        buttonRefreshNow = new JButton("Refresh Now");

    }
    public void setHostname(Vector v)
    {
        comboboxHostname.removeAllItems();
        for(int i = 0; i < v.size(); i ++)
        {
            comboboxHostname.addItem(v.get(i));
        }
    }
    public String getHostname()
    {
        return comboboxHostname.getSelectedItem().toString();
    }

    public void AddComponentToPane(Container paneLogin)  {
        paneLogin.setLayout(new GridBagLayout());
        GridBagConstraints constraint = new GridBagConstraints();
        try
        {
            nets = JProcess.listIPNets();
        }catch(SocketException se)
        {
            JOptionPane.showMessageDialog(paneLogin, se.getMessage());
        }
        String[]list = JProcess.listNICName(nets);
        comboNetCards = new JComboBox(list);

        constraint.fill = GridBagConstraints.HORIZONTAL;
        constraint.weightx = 0.5;

        constraint.ipadx = 30;
        constraint.insets = new Insets(2, 0, 3, 5);
        constraint.gridx = 0;
        constraint.gridy = 0;
        paneLogin.add(labelHostname, constraint);

        constraint.gridx = 0;
        constraint.gridy = 1;
        paneLogin.add(labelGateway, constraint);
        
        constraint.gridx = 0;
        constraint.gridy = 2;
        paneLogin.add(new JLabel("Update for host: "), constraint);


        constraint.gridx = 0;
        constraint.gridy = 3;
        paneLogin.add(new JLabel("Status: "), constraint);


        constraint.gridx = 0;
        constraint.gridy = 4;
        paneLogin.add(new JLabel("Time:"), constraint);

        constraint.gridx = 0;
        constraint.gridy = 5;
        paneLogin.add(labelRefreshtime, constraint);

        constraint.gridwidth = 10;
        constraint.ipadx = 200;
        constraint.gridx = 1;
        constraint.gridy = 0;
        paneLogin.add(comboboxHostname, constraint);
        

        constraint.gridx = 1;
        constraint.gridy = 1;
        paneLogin.add(comboNetCards, constraint);
        comboNetCards.setActionCommand("comboNetCard");
        comboNetCards.addActionListener(this);

        constraint.gridx = 1;
        constraint.gridy = 2;
        paneLogin.add(new JLabel(Main.host), constraint);

        constraint.gridx = 1;
        constraint.gridy = 3;
        paneLogin.add(labelStatus, constraint);

        constraint.gridx = 1;
        constraint.gridy = 4;
        paneLogin.add(labelTimeRemaining, constraint);
        
        constraint.ipadx = 0;

        constraint.gridx = 1;
        constraint.gridy = 5;
        paneLogin.add(buttonRefreshNow,constraint);
        buttonRefreshNow.setActionCommand("btnRefesh");
        buttonRefreshNow.addActionListener(this);
        //buttonRefreshNow.addActionListener(new MyRefreshActionListener());



        Main.Ethernet = (String)comboNetCards.getSelectedItem();
        Main.newIP = JProcess.getIP(nets, Main.Ethernet);
        labelStatus.setText((String)comboNetCards.getSelectedItem() + ":" + Main.newIP);

        permission = checkPermission();
        if(permission)
        {
            auto = new AutoUpdate(labelTimeRemaining,labelStatus,comboboxHostname,comboNetCards,buttonRefreshNow, Main.TimeUpdate);
            auto.t.start();
        }
        else
        {
            labelStatus.setText("You don't have permision to update this host");
            buttonRefreshNow.setEnabled(permission);
        }
    }

    public boolean checkPermission()
    {

        int idx = Main.host.lastIndexOf(".");
        if(idx >=0)
            Main.host = Main.host.substring(0,idx + 1);
        for(int i = 0; i < comboboxHostname.getItemCount(); i++)
        {
            String item = (String)comboboxHostname.getItemAt(i);
            if(item.equals(Main.host))
            {
                return true;
            }
        }
        return false;
    }

    public void CreateAndShowGUI() {
        frameRunning = new JFrame("DDNS Client");
        frameRunning.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.AddComponentToPane(frameRunning.getContentPane());
        frameRunning.setPreferredSize(new Dimension(700, 200));
        
        frameRunning.pack();
        frameRunning.setVisible(true);
        frameRunning.setLocationRelativeTo(null);

    }

    public void actionPerformed(ActionEvent e) 
    {
        if(e.getActionCommand().equals("comboNetCard"))
        {
            //JOptionPane.showMessageDialog(labelHostname, "change");
            Main.Ethernet = (String)comboNetCards.getSelectedItem();
            Main.newIP = JProcess.getIP(nets, Main.Ethernet);
            if(permission)
                labelStatus.setText((String)comboNetCards.getSelectedItem() + ":" + Main.newIP);
        }

        if(e.getActionCommand().equals("btnRefesh"))
        {
            MySendingInformation mysending = new MySendingInformation();
            Main.newIP = JProcess.getCurrentIP(Main.Ethernet);
            labelStatus.setText(Main.Ethernet + ": " + Main.newIP);
            if(Main.newIP == null || Main.newIP.equals("") || Main.newIP.equals("invalid NIC"))
            {
                JOptionPane.showMessageDialog(new JFrame(), "Current IP of " + Main.Ethernet + " is null","Error" , JOptionPane.ERROR_MESSAGE, null);
                return;
            }
            boolean done = mysending.Send();
            if(done == false)
                JOptionPane.showMessageDialog(new JFrame(), "Sending fail", "Error", JOptionPane.OK_OPTION, null);
            else
                JOptionPane.showMessageDialog(new JFrame(), "Sending success", "Information", JOptionPane.INFORMATION_MESSAGE, null);

        }

    }


}
